import numpy as np

from mainmethod import *

# import pandas as pd

if __name__ == '__main__':
    # flag = 0
    # # ecfinal = 10000
    # ecfinal = np.ceil(0.05 * 512 * 512)
    # namelist = np.array(
    #     [[10, 7, 6, 1, 2, ], [10, 4, 7, 2, 1, ], [2, 10, 6, 4, 9, ], [1, 4, 5, 7, 13, ], [3, 1, 4, 10, 5, ]], dtype=int)
    # k = 16
    # key = 9999
    # Img = Load_img('test')
    # print( Img.img_path)
    # keyset = namelist[1, :]
    # # for i in range(len(Img)):
    # img = Img[3]
    # pathname = Img.img_path[3]
    # name= pathname.split('.', 1)[0] + '.csv'
    # imgs = copy.deepcopy(img)
    # pd.DataFrame(img).to_csv('yuan_img.csv')
    # # img_hide, RateList, secretinfos, cm, PZlist,Rates,ec1 ,em,typemap,p,predx, v1, h1,secret_info= danci(imgs, flag, k, ecfinal)
    # # img_hide, RateList,ec1 = danci(imgs, flag, k, ecfinal,key,keyset)
    # img_hide, RateList, ec1 = danci2(imgs, flag, k, ecfinal, key)
    # #
    # # secretinfos = np.array(secretinfos,dtype=int)
    # #
    # # pd.DataFrame(Rates).to_csv('Rates.csv')
    # # pd.DataFrame(cm).to_csv('cm.csv')
    # #
    # new_img = copy.deepcopy(img_hide)
    # # pd.DataFrame(new_img).to_csv('new_img.csv')
    # # pd.DataFrame(secretinfos).to_csv('secretinfos.csv')
    # # pd.DataFrame(secret_info).to_csv('secret_info.csv')
    # # v, h = get_grad(new_img)
    # # ems, typemaps,p2,predx2 = get_em(new_img, flag, v, h)
    # # pd.DataFrame(em).to_csv('em2.csv')
    # # pd.DataFrame(ems).to_csv('em.csv')
    # # pd.DataFrame(typemaps).to_csv('typemap.csv')
    # # pd.DataFrame(typemap).to_csv('typemap2.csv')
    # # secret = recover(ems, cm, new_img, Rates, flag, PZlist, typemaps,ecfinal)
    # # secret = np.array(secret,dtype=int)
    # # print(secret == secretinfos)
    # print('ec', RateList,ec1)
    # if int(np.sum(ec1)) == 0:
    #     print('第二次插入')
    #     k = 16
    #     flags = 1
    #     # img_hide, RateList, ec = danci(new_img, flags, k, ecfinal,key,keyset)
    #     img_hide, RateList, ec = danci2(new_img, flags, k, ecfinal, key)
    #     # pd.DataFrame(new_img).to_csv('./newimg.csv')
    #     psnr = cal_psnr(img, img_hide, ec)
    #     print('psnr', psnr)

    #
    #
    # x_size = np.linspace(0.1, 0.5, 21)
    # x_size = np.linspace(0.1, 0.7, 7)
    x_size = np.linspace(0.01, 0.17, 17)

    # y_size = np.zeros(21)
    Img = Load_img('test')

    # for j in range(1,len(Img.img_path)):
    print(Img.img_path)
    j =1
    img = Img[j]
    pathname = Img.img_path[j]
    name = pathname.split('.', 1)[0] + '.csv'
    imgs = copy.deepcopy(img)
    index = 0

    bestkey = pd.read_csv('./bsetfeature.csv').values
    bestkey = bestkey[:, 1:]
    bestkey = np.array(bestkey[j, :], dtype=int)
    bestkey = bestkey[bestkey > 0]
    keyset = bestkey

    update_value = pd.read_csv('./update7.csv').values
    update_value = np.array(update_value[:, 1:], dtype=int)
    singe_value =  update_value[j,:]
    singe_value = singe_value[singe_value>0]
    y_size = np.zeros(17)
    for i in range(len(singe_value)):
        if j !=1:
            keylist = np.arange(3, 22)
        else:
            keylist = np.linspace(15,30,17)
        key = keylist[singe_value[i]]*8
        print(key,singe_value[i],keylist)
        # key = 60
        flag = 0
        print("第{}论".format(i))
        # ecfinal = np.ceil(x_size[singe_value[i]] * 512 * 512)
        ecfinal = np.ceil(x_size[singe_value[i]] * 512 * 512)
        k = 16
        img_hide, RateList, ec1 = danci(imgs, flag, k, ecfinal, key, keyset)
        new_img = copy.deepcopy(img_hide)
        print('ec', RateList)
        if int(np.sum(ec1)) == 0:
            print('第二次插入')
            k = 16
            flags = 1
            new_img, RateLis2t, ec = danci(new_img, flags, k, ecfinal, key, keyset)
            # pd.DataFrame(new_img).to_csv('./newimg.csv')
            psnr = cal_psnr(img, new_img, ec)
            print('psnr', psnr)
            #     y_size[singe_value[i]] = psnr
            # pd.DataFrame(y_size).to_csv('./2022-3-10-2/' + name)
    # plt_make(x_size, y_size)
